#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Taotie Ren

salary = input("Input your salary:")

if salary.isdigit():
    salary = int(salary)
else:
    exit("Invaild data type ...")

welcome_msg = 'Welcome to Tom shopping mall.'.center(50,'*')
print(welcome_msg)

exit_flag = False

product_list = [
    ('Iphone',5888),
    ('Mac Air',8000),
    ('Mac Pro',9000),
    ('XiaoMi 2',1699),
    ('Coffee',30),
    ('Tesla',820000),
    ('Bike',700),
    ('Cloth',200),
]
shop_car =[]
while exit_flag is not True:
    print("product list".center(50,'*'))
    #for product_item in product_list:
    #    p_name,p_price = product_item
    for item in enumerate(product_list):
        index = item[0]
        p_name = item[1][0]
        p_price = item[1][1]
        print(index,'.',p_name,p_price)

    user_choice = input("[q=quit,c=check]What do you want to buy?:")
    if user_choice.isdigit():#肯定是选择商品
        user_choice = int(user_choice)
        if user_choice < len(product_list):
            p_item = product_list[user_choice]
            if p_item[1] <= salary:#买得起
                shop_car.append(p_item)#放入购物车
                salary -= p_item[1]#减钱
                print("Added [%s] into shop car, You current balance is \033[31:1m[%s]\033[0m"%(p_item,salary))
            else:
                print("Your balance is [%s],cannot afford this ..."%salary)
    else:
        if user_choice == 'q' or user_choice == 'quit':
            print("purchased products as blelow".center(50,'*'))
            for item in shop_car:
                print(item)
            print("END".center(50,'*'))
            print("Your balance is \033[41:1m[%s]\033[0m"%salary)
            exit_flag = True


